#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Code to rotate or set wallpaper under windows
# Copyright (C) Andrew K. Milton 2007 - 2013
# Released under a 2-clause BSD License
# See: http://www.opensource.org/licenses/bsd-license.php

# Multi monitor detection based on http://code.activestate.com/recipes/460509/

# This will handle any configuration of an arbitrary number of monitors,
# including offset boundaries.


import os
import sys
import time

from optparse import OptionParser
from ConfigParser import SafeConfigParser

# I recommend you create a pywallpaper.conf file that looks something
# like this to store the directories in rather than specifying -d
# multiple times on the command line.
#
# NB you can specify paths per monitor. Any monitor number without its
# own paths will get the global paths. Monitors start at 0 which is always
# the primary monitor.
#
# Some options don't play nicely together.

"""
[global]
Blending = True
BlendRatio = 0.40
Crop = False
Fill = True
Gradient = False
PreRotate = True

; Prefixing a directory with '+' indicates that all subfolders should be recursed into
[directories]
paths = C:\Documents and Settings\akm\My Documents\My Pictures\gb
        +C:\Documents and Settings\akm\My Documents\My Pictures\Ralph

[monitor_0]
paths = C:\Documents and Settings\akm\My Documents\My Pictures\Wide Screen
[monitor_1]
paths = C:\Documents and Settings\akm\My Documents\My Pictures\Landscapes
"""
from Desktop.WindowsDesktop import WindowsDesktop as Desktop


class Wallpaper(object):
	"""
	The master wallpaper object
	"""
	
	def getConfigFileOptions(self, options):
		configFile = 'pywallpaper.conf'

		if options.configFile:
			configFile = options.configFile

		dirs = options.directories

		self.config = SafeConfigParser(
			defaults = {'Blending': False,
						'BlendRatio': 1.0,
						'Crop': False,
						'Fill': False,
						'Gradient': False,
						'PreRotate': False,
						'Spanning': False,
						'StackMode': False}
			)
		
		self.config.readfp(open(configFile))

	def getCommandLineOptions(self):
		parser = OptionParser()
		parser.add_option("-t", "--time", dest="change_time",
						  help = "Change wallpaper time in minutes (0 = change once and exit [default])",
						  default = 0, type = "int")
		parser.add_option("-i", "--image", dest="singleImage", default = None,
						  help = "Set wallpaper to this image and exit (overrides -d)")

		parser.add_option("-d", "--directory", dest="directories", default = [], 
						  action="append", type="string",
						  help = "Add an image directory")

		parser.add_option("-c", "--config", dest="configFile", default = None,
						  help = "path to alternate config file (default <working dir>/pywallpaper.conf)")

		parser.add_option("-w", "--workingdir", dest="cwd", default=".",
						  help = "Working Directory (default .)")

		(options, args) = parser.parse_args()
		return (options, args)

	def go(self):
		options, args = self.getCommandLineOptions()
		self.getConfigFileOptions(options)
		desktop = Desktop(self.config)		
		
		if options.cwd and options.cwd != '.':
			os.cwd(cwd)
		if options.singleImage:
			desktop.setWallPaper(options.singleImage)
			
		elif not options.change_time:
			desktop.generateWallpaper()
		else:
			sleepTime = options.change_time * 60.0
			while True:
				desktop.setWallPaperFromDirList()
				time.sleep(sleepTime)

if __name__ == '__main__':
	from ProcessElevation import elevateSelf
##	elevateSelf()
	d = Wallpaper()
	d.go()
